package com.bw.controller.order;


import com.bw.resp.ResponseResult;
import com.bw.service.order.IPiOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *  订单图纸
 * @author yzz
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/orderpic")
public class PiOrderPicController {

    @Autowired
    private IPiOrderService orderService;



    /**
     * @Description:图片的导入
     * @Author: 李钊毅
     * @Date: 2020/6/22 17:55
     * @param file:
     * @param code:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("/importPhotos")
    public ResponseResult importPhotos(MultipartFile file,String code){
        ResponseResult success = ResponseResult.SUCCESS();
        try {
            orderService.improtPic(file,code);
        }catch (Exception e){
            success=ResponseResult.FAIL();
        }
        return success;
    }
    /**
     * @Description:图片的显示
     * @Author: 李钊毅
     * @Date: 2020/6/22 17:55
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("/showPhoto/{orderCode}/{itemCode}")
    public ResponseResult showPhoto(@PathVariable("orderCode")String orderCode, @PathVariable("itemCode")String itemCode, HttpServletResponse response){
        ResponseResult success = ResponseResult.SUCCESS();
        try {
            InputStream is = orderService.preView(orderCode,itemCode);
            int len;
            byte[] buffer=new byte[1024];
            ServletOutputStream outputStream = response.getOutputStream();
            while ((len=is.read(buffer))>0){
                outputStream.write(buffer,0,len);
            }
            outputStream.close();
        }catch (Exception e){
            success=ResponseResult.FAIL();
            success.setMessage("没有找到图片");
        }
        return success;
    }
}
